import { getUserStore } from "@/common/hox/userStore"
import { PlatformEnum } from "@/utils/enum"
import Taro from "@tarojs/taro"
import { inWechat } from "."
import auth from "./auth"
import config from "./config"

type ConfigType = AppConfigType & {
    appId: string | undefined,
    smallAppId: string | undefined,
    debug: boolean | undefined,
    environment?: string,
    serviceUrl: string | undefined,
    useTest?: boolean,
    thirdPart?: boolean,
    programTitle?: string,
}

const appConfig = (function () {
    let configs: Omit<ConfigType, 'appPlatformType'> = {
        appId: config.appId,
        smallAppId: config.smallAppId,
        debug: config.debug,
        appConfigId: config.appConfigId,
        merchantId: config.merchantId,
        businessMerchantAppConfigId: config.merchantId,
        serviceUrl: config.serviceUrl,
        auth,
        baseUrl: config.baseUrl,
    }

    return {
        ...configs,
        get toService() {
            const { userId } = getUserStore?.()?.userInfo || {}
            if (userId) {
                return () => {
                    Taro.navigateTo({ url: '/pages/my/service/index' })
                }
            }
            return undefined
        },
        get appPlatformType() {
            if (process.env.TARO_ENV === 'weapp') {
                return PlatformEnum.WeChatSmall
            }
            if (process.env.TARO_ENV === 'tt') {
                return PlatformEnum.ToutiaoSmall
            }
            if (process.env.TARO_ENV === 'h5') {
                if (inWechat()) return PlatformEnum.WeChatPub
            }
            return PlatformEnum.H5
        },
    }
})()

export default appConfig